﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;

namespace Perm
{
    public partial class Comment : ChildWindow
    {        
        bool nameGood = false,
             textGood = false;
        public string city = "";
        public int commentId;
        //Road info
        public string roadPodr = null;
        public string roadStreet = null;        
        public int roadReit = 0;
        //Gas info
        public string gasOrg = null;
        public string gasAdress = null;
        public int gasReit = 0;
        //Carwash info
        public string сarwashOrg = null;
        public string сarwashAdress = null;
        public int сarwashReit = 0;
        string commentText, commentName, globalType, name="";
        DateTime thisDate = new DateTime();
        //Дочерние окна
        messageBox msg = new messageBox();
        public Comment()
        {
            InitializeComponent();
        }        
        void Load(ObservableCollection<_Comments.CommentsComment> list)
        {
            if (commentId != 0)
            {
                if (roadStreet != null | roadPodr != null)
                {
                    WindowComment.Title = "Комментарии. " + roadStreet + " " + roadPodr + " рейтинг: " + roadReit.ToString();
                    roadPodr = null;
                    roadStreet = null;
                    roadReit = 0;
                }
                if (gasOrg != null | gasAdress != null)
                {
                    WindowComment.Title = "Комментарии. " + gasAdress + " " + gasOrg + " рейтинг: " + gasReit.ToString();
                    gasOrg = null;
                    gasAdress = null;
                    gasReit = 0;
                }
                if (сarwashOrg != null | сarwashAdress != null)
                {
                    WindowComment.Title = "Комментарии. " + сarwashAdress + " " + сarwashOrg + " рейтинг: " + сarwashReit.ToString();
                    сarwashOrg = null;
                    сarwashAdress = null;
                    сarwashReit = 0;
                }
            }
            else WindowComment.Title = "Комментарии";
            comments.Children.Clear();
            int i = 0;
            while (i <= list.Count - 1)
            {
                Grid grid = new Grid();
                StackPanel stack = new StackPanel();
                TextBlock name = new TextBlock();
                TextBlock data = new TextBlock();
                TextBox text = new TextBox();
                grid.Name = "grid" + i.ToString();                
                grid.HorizontalAlignment = HorizontalAlignment.Stretch;
                if (i == 0)
                {
                    grid.Margin = new Thickness(0, 10, 0, 10);
                }
                else
                {
                    grid.Margin = new Thickness(0, 0, 0, 10);
                }
                name.Name = "name" + i.ToString();
                name.Width = 180;                
                name.HorizontalAlignment = HorizontalAlignment.Left;
                name.VerticalAlignment = VerticalAlignment.Top;
                name.TextTrimming = TextTrimming.WordEllipsis;
                name.TextWrapping = TextWrapping.Wrap;
                name.FontWeight = FontWeights.Bold;
                name.Foreground = new SolidColorBrush(Color.FromArgb(255,160,160,160));
                data.Name = "data" + i.ToString();
                data.Width = 160;
                data.Height = 20;
                data.HorizontalAlignment = HorizontalAlignment.Right;
                data.VerticalAlignment = VerticalAlignment.Top;
                data.TextTrimming = TextTrimming.WordEllipsis;
                data.Foreground = new SolidColorBrush(Colors.DarkGray);
                text.Name = "text" + i.ToString();
                text.HorizontalAlignment = HorizontalAlignment.Stretch;
                text.VerticalAlignment = VerticalAlignment.Stretch;
                text.VerticalContentAlignment = VerticalAlignment.Top;
                text.HorizontalContentAlignment = HorizontalAlignment.Left;
                text.TextWrapping = TextWrapping.Wrap;
                text.FontSize = 10;
                text.BorderThickness = new Thickness(0, 0, 0, 0);
                text.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 200, 200, 200));
                text.Background = new SolidColorBrush(Color.FromArgb(30, 255, 255, 255));
                text.FontStyle = FontStyles.Italic;
                text.Margin = new Thickness(4);
                name.Text = list[i].name;
                data.Text = list[i].data;
                text.Text = list[i].comment;
                grid.Children.Add(name);
                grid.Children.Add(data);
                stack.Children.Add(grid);
                stack.Children.Add(text);             
                comments.Children.Add(stack);
                i++;
            }
        }

        void getsetComments(string type)
        {             
            _Comments.CommentsClient proxyComments = new _Comments.CommentsClient();
            proxyComments.DoWorkCompleted += new EventHandler<_Comments.DoWorkCompletedEventArgs>(proxyComments_DoWorkCompleted);
            proxyComments.DoWorkAsync(city, commentId, nameForm.Text, textForm.Text, type);
            proxyComments.CloseAsync();
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            globalType = "save";
            if (!Regex.IsMatch(nameForm.Text, @"^[\w''-''--'!?_,.\s\d]{2,40}$"))
            {
                nameGood = false;
                nameError.Content = "некорректно введено имя";
                nameError.Visibility = Visibility.Visible;
            }
            else
            {
                nameGood = true;
                nameError.Visibility = Visibility.Collapsed;
            }
            if (!Regex.IsMatch(textForm.Text, @"^[\w''-'!?;:$&^()+-=_,.\s\d]{2,1000}$"))
            {
                textGood = false;
                textError.Content = "некорректно введён комментарий";
                textError.Visibility = Visibility.Visible;
            }
            else
            { 
                textGood = true;
                textError.Visibility = Visibility.Collapsed;
            }
            if (nameGood & textGood)
            {
                name = nameForm.Text;
                getsetComments(globalType); 
                this.Close();
            }
        }

        void proxyComments_DoWorkCompleted(object sender, _Comments.DoWorkCompletedEventArgs e)
        {
            switch (globalType)
            {
                case "save":
                    {
                        //msg.info.Text = "Спасибо за коментариий.";
                        //msg.Show();
                        MessageBox.Show(name + " cпасибо, за комментарий.");
                        break;
                    }
                case "load":
                    {
                        Load(e.Result);
                        break;
                    }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            globalType = "load";
            getsetComments(globalType);
        }

        private void WindowComment_Closed(object sender, EventArgs e)
        {
            nameForm.Text = "";
            textForm.Text = "";
            nameGood = false;
            textGood = false;
            nameError.Visibility = Visibility.Collapsed;
            textError.Visibility = Visibility.Collapsed;
        }
    }
}

